﻿using System.IO;
using Microsoft.Win32;

namespace roysland.net.xcom.core.DataAccess
{
    /// <summary>
    /// This static class provides access to read and write to game files
    /// </summary>
    public static class XComDataAccess
    {
        #region Game Files

        #region OBDATA.DAT
        public static byte[] GetObData()
        {
            return ReadByteArrayFromString(GetGameFolder() + @"GEODATA\OBDATA.DAT");
        }

        public static void SetObData(byte[] buffer)
        {
            WriteByteArrayToString(buffer, GetGameFolder() + @"GEODATA\OBDATA.DAT");
        }
        #endregion

        #region GEOSCAPE.EXE
        public static byte[] GetGeoscapeExeData()
        {
            return ReadByteArrayFromString(GetGameFolder() + @"UFOEXE\GEOSCAPE.EXE");
        }

        public static void SetGeoscapeExeData(byte[] buffer)
        {
            WriteByteArrayToString(buffer, GetGameFolder() + @"UFOEXE\GEOSCAPE.EXE");
        }
        #endregion

        #region ENGLISH.DAT
        public static byte[] GetEnglishData()
        {
            return ReadByteArrayFromString(GetGameFolder() + @"GEODATA\ENGLISH.DAT");
        }

        public static void SetEnglishData(byte[] buffer)
        {
            WriteByteArrayToString(buffer, GetGameFolder() + @"GEODATA\ENGLISH.DAT");
        }
        #endregion

        #endregion

        #region GetGameFolder
        /// <summary>
        /// Returns the path to game files
        /// </summary>
        public static string GetGameFolder()
        {
            
            string path;

            // Access registry key to get proper path if using Steam
            var key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Valve\\Steam");
            if (key != null)
            {
                path = key.GetValue("SteamPath").ToString();
                path = path.Replace('/', '\\');
                path += @"\steamapps\common\xcom ufo defense\XCOM\";
            }
            else
                path = @"C:\Program Files\Steam\steamapps\common\xcom ufo defense\XCOM\";

            //TODO: Use alternative checks if game is not Steam purchase

            if (key != null) key.Close();

            return path;
        }
        #endregion

        #region IO Functions
        /// <summary>
        /// Get the binary representation of the given file
        /// </summary>
        /// <param name="filename">File to open</param>
        /// <returns>Byte array of file content</returns>
        public static byte[] ReadByteArrayFromString(string filename)
        {
            //TODO: Check if file exist and throw exception if not

            var fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            var br = new BinaryReader(fs);
            var numBytes = new FileInfo(filename).Length;
            byte[] buffer = br.ReadBytes((int)numBytes);
            fs.Close();

            return buffer;
        }

        /// <summary>
        /// Write to file
        /// </summary>
        /// <param name="buffer">Byte array to write</param>
        /// <param name="filename">Target filename</param>
        public static void WriteByteArrayToString(byte[] buffer, string filename)
        {
            var fs = new FileStream(filename, FileMode.Open, FileAccess.Write);
            var bw = new BinaryWriter(fs);
            bw.Write(buffer);
            fs.Close();
        }
        #endregion
    }
}
